#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    //注册HImage
    //qRegisterMetaType<HImage>("HImage");
	
    //将halcon窗口加载到控件中
    hv_WindowHandle = ui.gv_showImage->SetHalconWnd();

    //初始化相机代理类
    cameraProxy.reset(new CameraProxy());

    connect(cameraProxy.data(), &CameraProxy::Message, this, [=](QString type, QString message) {
        if (type == "Error")
            QMessageBox::critical(this, "Error", message);
        else if (type == "Warning")
            QMessageBox::warning(this, "warning", message);
        else if (type == "Success")
            QMessageBox::information(this, "information", message);
        });
}

MainWindow::~MainWindow()
{}


void MainWindow::closeEvent(QCloseEvent *event) 
{
    emit cameraProxy->CloseCamera_Sig();
}

/*
 *@brief:打开相机
 */
void MainWindow::on_btn_opencamera_clicked()
{
    emit cameraProxy->OpenCamera_Sig();

    connect(cameraProxy.data(), &CameraProxy::SendImage, this, &MainWindow::ShowImage_Slot);
}


/*
 *@brief: 显示相机预览
 */
void MainWindow::ShowImage_Slot()
{
    ui.gv_showImage->showImage(Hik_Driver::sendImage);
}

/*
 *@brief: sild设置曝光
 */
void MainWindow::on_slid_exposure_valueChanged(int value)
{
    emit cameraProxy->CamSetExposureTime_Sig(value);

    ui.lab_exposure_value->setStyleSheet("QLabel { color : red; }");
    ui.lab_exposure_value->setText("曝光:" + QString::number(value));
}

/*
 *@brief: sild设置增益
 */
void MainWindow::on_sild_gain_valueChanged(int value)
{
    emit cameraProxy.data()->CamSetGain_Sig(value);
    
    ui.lab_gain_value->setStyleSheet("QLabel { color : red; }");
    ui.lab_gain_value->setText("增益:" + QString::number(value));
}


/*
 *@brief: 打开离线图片
 *
 */
void MainWindow::on_actionOpenImageFile_triggered()
{
    QString path = QFileDialog::getOpenFileName(this, "Open Image", QDir::homePath(), "*.jpg *.bmp *.png");
    if (!path.isEmpty()) 
    {
        //绑定ho_Image
        ReadImage(&ho_Image, path.toStdString().c_str());

        ui.gv_showImage->showImage(ho_Image); 
    }
    else
    {
        qWarning() << u8"当前文件路径存在问题";
    }
}

/*
 *@brief: 打开标定界面
 */
void MainWindow::on_actionCalibrate_triggered()
{
    //标定窗口
    calibrate = std::make_unique<Calibrate>();
    
    if(calibrate)
        calibrate->show();
    else
    {
        calibrate = std::make_unique<Calibrate>();
        calibrate->show();
    }
}

void MainWindow::on_btn_drawmodel_clicked()
{
    //ui.gv_showImage->Draw_Rectangle();
    //ui.gv_showImage->Draw_Circle();
    ui.gv_showImage->Draw_RectangleRotate();
}






